package com.zkthink.ceres.product.strategy;

import com.zkthink.ceres.order.dto.OrderProductDTO;
import com.zkthink.ceres.product.dto.ChargeDetailDTO;
import com.zkthink.ceres.product.strategy.FirstItemChargeDetailStrategy;

import java.util.List;

/**
 * <p>
 * 首重运费计算方式
 * </p>
 *
 * @author vivi.huang
 * @since 2020/5/13
 */
public class FirstWeightChargeDetailStrategy extends FirstItemChargeDetailStrategy {

    /**
     * 首重计算方式
     * W(总重量),fw(首重),r1(首重费用),sw(续重),r2(续重费用)
     * W < fw -> r1
     * W < fw -> r1 + ceil((W - fw) / sw) * r2
     *
     * @return
     */
    @Override
    public Long calculate(List<OrderProductDTO> orderProductDTOList, ChargeDetailDTO chargeDetailDTO) {

        Double totalWeight = getTotalWeight(orderProductDTOList);

        if (totalWeight < chargeDetailDTO.getFirstChargeForUnit()) {
            return chargeDetailDTO.getFirstChargeCost();
        }

        if (totalWeight > chargeDetailDTO.getFirstChargeForUnit()) {
            return calculateWithBiggerThanFirstForUnit(chargeDetailDTO, totalWeight);
        }
        return chargeDetailDTO.getFirstChargeCost();
    }

    private Double getTotalWeight(List<OrderProductDTO> orderProductDTOList) {
        return orderProductDTOList.stream().map(s -> s.getWeight()).reduce(0d, (acc, item) ->  acc + item);
    }
}